package me.ingxin.android.tools.app.drawer

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.viewpager2.widget.ViewPager2
import me.ingxin.android.tools.app.R
import me.ingxin.android.tools.app.databinding.ActivityDrawerBinding
import me.ingxin.android.tools.app.utils.logD
import me.ingxin.android.view.DrawerLayout

/**
 * Created by ingxin on 2022/5/25
 */
class DrawerActivity : AppCompatActivity() {

    private val binding by lazy { ActivityDrawerBinding.inflate(layoutInflater) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        supportActionBar?.hide()

        supportFragmentManager.beginTransaction()
            .replace(R.id.fg_container, Tab1Fragment())
            .commitAllowingStateLoss()

        binding.tab1.setOnCheckedChangeListener { buttonView, isChecked ->
            if (isChecked) {
                supportFragmentManager.beginTransaction()
                    .replace(R.id.fg_container, Tab1Fragment())
                    .commitAllowingStateLoss()
            }
        }

        binding.tab2.setOnCheckedChangeListener { buttonView, isChecked ->
            if (isChecked) {
                supportFragmentManager.beginTransaction()
                    .replace(R.id.fg_container, Tab2Fragment())
                    .commitAllowingStateLoss()
            }
        }

        binding.drawer.addDrawerListener(object : DrawerLayout.DrawerListener {
            override fun onDrawerSlide(slidePercent: Float, dx: Int) {
               ViewCompat.offsetLeftAndRight(binding.contentView, dx)

            }

            override fun onDrawerOpened() {
                logD("onDrawerOpened")
            }

            override fun onDrawerClosed() {
                logD("onDrawerClosed")
            }

            override fun onDrawerStateChanged(newState: Int) {
                logD("onDrawerStateChanged$newState")
            }
        })

        setDrawerAdapter()

    }

    override fun onBackPressed() {
        if (binding.drawer.isOpened()) {
            binding.drawer.closeDrawer(true)
        } else {
            super.onBackPressed()
        }
    }

    private fun setDrawerAdapter() {
        binding.drawerPager.orientation = ViewPager2.ORIENTATION_HORIZONTAL
        binding.drawerPager.adapter = DrawerPageAdapter().apply {
            for (i in 1..2) {
                datas.add("drawer PAGE $i")
            }
        }
    }


}